package com.ma.auth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ma.auth.service.SysRoleService;
import com.ma.common.result.Result;
import com.ma.model.system.SysRole;
import com.ma.vo.system.AssginRoleVo;
import com.ma.vo.system.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "角色管理的实现")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    //查询所有角色和当前用户所属角色
    @ApiOperation("根据用户获取角色信息")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId){
        Map<String, Object> map = sysRoleService.findRoleDateByUserId(userId);
        if(map == null){
            return Result.fail().message("未查到角色信息和用户所属角色");
        }
        return Result.ok(map);
    }


    //为用户分配角色信息
    @ApiOperation("为用户分配角色信息")
    @PostMapping("doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo){
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }

    //查询所有角色
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public Result<List<SysRole>> findAll(){
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }
    //条件分页查询
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")//page  当前页  limit  每页显示记录数  SysRoleQueryVo 条件对象
    public Result<Page<SysRole>> pageQueryRole(@PathVariable Long page,
                                  @PathVariable Long limit,
                                  SysRoleQueryVo sysRoleQueryVo){
        Page<SysRole> rolePage = sysRoleService.limit(page, limit, sysRoleQueryVo);
        return Result.ok(rolePage);
    }
    //添加角色
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping("/save")
    public Result save(@RequestBody SysRole sysRole){
        boolean isSuccess = sysRoleService.save(sysRole);
        if (isSuccess){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //根据id查询角色
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询角色")
    @GetMapping("/get/{id}")
    public Result<SysRole> getByID(@PathVariable Long id){
        SysRole sysRole = sysRoleService.getById(id);
        if(sysRole != null)
        return Result.ok(sysRole);
        return Result.fail();
    }
    //修改角色
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("修改角色")
    @PutMapping("/update")
    public Result update(@RequestBody SysRole sysRole){
        boolean isSuccess = sysRoleService.updateById(sysRole);
        if(isSuccess){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //删除角色
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("删除角色")
    @DeleteMapping("remove/{id}")
    public Result deleteById(@PathVariable Long id){
       return sysRoleService.removeById(id) ? Result.ok() : Result.fail();
    }
    //批量删除
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        return sysRoleService.removeByIds(ids) ? Result.ok() : Result.fail();
    }
}
